﻿using System;
using System.Collections.Generic;
using ThingyCloudWPF.Creatures;

namespace ThingyCloudWPF.Network.Messages
{
	public class AvatarMergeMessage : Message
	{
		/// <summary>
		/// The new avatar
		/// </summary>
		private Avatar avatar;

		/// <summary>
		/// A list of avatars that the new one was created from
		/// </summary>
		private List<Avatar> mergedFrom;

		/// <summary>
		/// Construct the message. We need the new avatar and a list of avatars it was merged from.
		/// </summary>
		/// <param name="newAvatar"></param>
		/// <param name="mergedFrom"></param>
		public AvatarMergeMessage(Avatar avatar, List<Avatar> mergedFrom)
		{
			this.avatar = avatar;
			this.mergedFrom = mergedFrom;
		}

		/// <summary>
		/// Build the message
		/// </summary>
		/// <returns></returns>
		public override string ToMessageString()
		{
			string msg = "action:merge;oldid:";

			foreach (Avatar old in mergedFrom)
				msg += old.ID + ",";

			// Get rid of last comma
			msg = msg.Substring(0, msg.Length - 1) + ";";

			msg += string.Format(
					"id:{0};crid:{1};var:{2};x:{3};y{4};r:{5};w:{6};h:{7};",
					avatar.ID,
					avatar.Properties.ID,
					avatar.Variant.ID,
					Globals.ToAbsX(avatar.X),
					Globals.ToAbsY(avatar.Y),
					avatar.Direction,
					avatar.Width,
					avatar.Height
					);

			return msg;
		}
	}
}
